﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using HRSystem.Utility;
using HRSystem.Entity;

namespace HRSystem.DBAccess
{
    public class Access
    {
        public string CheckNull(object value)
        {
            string s = Convert.ToString(value);
            if (String.IsNullOrEmpty(s))
                return string.Empty;
            else
                return s;
        }

        public double CheckDoubleNull(object value)
        {
            if (value == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToDouble(value);
            }
        }

        public int CheckIntNull(object value)
        {
            if (value == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(value);
            }
        }

        public DateTime CheckDatetimeNull(object value)
        {
            if (value == DBNull.Value)
            {
                return DateTime.Now;
            }
            else
            {
                DateTime mytime;
                try
                {
                    mytime = Convert.ToDateTime(value);
                }
                catch (Exception)
                {
                    mytime = new DateTime(1900,1,1);
                }

                return mytime;
            }
        }

        public DateTime CheckDatetimeNull(object value,DateTime defaultValue)
        {
            if (value == DBNull.Value)
            {
                return DateTime.Now;
            }
            else
            {
                DateTime mytime;
                try
                {
                    mytime = Convert.ToDateTime(value);
                }
                catch (Exception)
                {
                    mytime = defaultValue;
                }

                return mytime;
            }
        }

        /// <summary>
        /// 获取数据库的时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetDBDateTime()
        {
            string sql = "select now()";
            DateTime dbTime = DateTime.Now;


            using (MySqlDataReader reader = MySqlUtil.GetDataReader(sql,null))
            {
                if (reader.Read())
                {
                    dbTime = Convert.ToDateTime(reader[0]);
                }
            }

            return dbTime;
        }

        public int GetLastID(string tableName)
        {
            string sql = String.Format("select ID from {0} order by ID DESC limit 0,1",tableName);
            int id = 0;
            using (MySqlDataReader reader = MySqlUtil.GetDataReader(sql,null))
            {
                if (reader.Read())
                {
                    id = Convert.ToInt32(reader["ID"]);
                }
            }
            return id;

        }

        public bool IsConnectedDB()
        {
            return true;
        }
    }
}
